package GUI.Ventas.Facturacion;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import sale.entities.FacturaVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableItem;

import GUI.Ventas.selectClient;

public class CerrarVenta extends Dialog {

	protected Object result;
	protected Shell shell;
	SaleVO venta;
	private Table table;
	private TableColumn tblclmnArticulosConsumidos;
	private TableColumn tblclmnCantidad;
	private TableColumn tblclmnPrecioUnitario;
	private Label label;
	private Label lblPrecioTotalDe;
	private Label totalSinDescuento;
	private Button btnAgregarClienteA;
	private Button descuentos;
	private Button btnAceptarEImprimir;
	private Label lblCliente;
	private Label cliente;
	private Label lblDescuentos;
	private Label descuentos_1;
	private Label totalConDescuentos;
	private Label td;
	private Label label_2;
	private Button btnCancelar;
	private TableItem tableItem;
	private TableColumn tblclmnPrecioTotal;
	FacturaVO fact;
	private Label lblNumeroDeFactura;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public CerrarVenta(Shell parent, int style, SaleVO sale, FacturaVO f) {
		super(parent, style);
		setText("Cerrar Venta");
		venta=sale;
		fact=f;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws EmptySaleExcpetion 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException, EmptySaleExcpetion {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws EmptySaleExcpetion 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException, EmptySaleExcpetion {
		SaleProxyMgt sale = ServiceFacade.getInstance().getSaleProxyMgt();
		if(fact==null){
		fact = sale.close(venta);
		
		 }
	
		shell = new Shell(getParent(), getStyle());
		shell.setSize(616, 473);
		shell.setText(getText());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 22, 411, 132);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		tblclmnArticulosConsumidos = new TableColumn(table, SWT.NONE);
		tblclmnArticulosConsumidos.setWidth(156);
		tblclmnArticulosConsumidos.setText("Articulos Consumidos");
		
		tblclmnCantidad = new TableColumn(table, SWT.NONE);
		tblclmnCantidad.setWidth(54);
		tblclmnCantidad.setText("Cantidad");
		
		tblclmnPrecioUnitario = new TableColumn(table, SWT.NONE);
		tblclmnPrecioUnitario.setWidth(100);
		tblclmnPrecioUnitario.setText("Precio Unitario");
		
		tblclmnPrecioTotal = new TableColumn(table, SWT.NONE);
		tblclmnPrecioTotal.setWidth(100);
		tblclmnPrecioTotal.setText("Precio Total");
		
	
		
		
		label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 184, 585, 20);
		
		lblPrecioTotalDe = new Label(shell, SWT.NONE);
		lblPrecioTotalDe.setBounds(238, 174, 222, 14);
		lblPrecioTotalDe.setText("Precio Total de Articulos Consumidos");
		
		totalSinDescuento = new Label(shell, SWT.NONE);
		totalSinDescuento.setBounds(466, 174, 119, 14);
		totalSinDescuento.setText(String.valueOf(fact.getTotal()));
		
		btnAgregarClienteA = new Button(shell, SWT.NONE);
		btnAgregarClienteA.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//Constructor: Shell parent, int style, OrderVO o, SaleVO s, int opcion
				selectClient select = new selectClient(shell, shell.getStyle(), null, fact, 1);
				shell.setVisible(false);
				select.open();
			}
		});
		btnAgregarClienteA.setBounds(288, 322, 203, 28);
		btnAgregarClienteA.setText("Asociar Factura a Cliente");
		
		descuentos = new Button(shell, SWT.NONE);
		descuentos.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SetDescuento set = new SetDescuento (shell, shell.getStyle(), fact);
				set.open();
				shell.dispose();
			}
		});
		descuentos.setBounds(288, 356, 203, 28);
		descuentos.setText("Descuentos Adicionales");
		
		btnAceptarEImprimir = new Button(shell, SWT.NONE);
		btnAceptarEImprimir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SaleProxyMgt sale = ServiceFacade.getInstance().getSaleProxyMgt();
				try {
					sale.commit(fact);
					System.out.println("commited");
					shell.setVisible(false);
					shell.dispose();
					
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnAceptarEImprimir.setBounds(119, 413, 132, 28);
		btnAceptarEImprimir.setText("Aceptar e Imprimir");
		
		lblCliente = new Label(shell, SWT.NONE);
		lblCliente.setBounds(238, 210, 203, 14);
		lblCliente.setText("Cliente");
		
		cliente = new Label(shell, SWT.NONE);
		cliente.setBounds(465, 210, 59, 14);
		if(fact.getClient()!=null){
			cliente.setText(fact.getClient().getName() + fact.getClient().getLastName());
		}
		else{
			
		
		cliente.setText("-");
		}
		lblDescuentos = new Label(shell, SWT.NONE);
		lblDescuentos.setBounds(238, 231, 73, 14);
	
		lblDescuentos.setText("Descuentos");
		
		descuentos_1 = new Label(shell, SWT.NONE);
		descuentos_1.setBounds(465, 231, 59, 14);
		//if(fact.getDescuento())
		descuentos_1.setText(String.valueOf(fact.getDescuento() + "%"));
		
		totalConDescuentos = new Label(shell, SWT.NONE);
		totalConDescuentos.setBounds(238, 255, 171, 14);
		totalConDescuentos.setText("Total con Descuentos");
		
		Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
		label_1.setBounds(447, 171, 13, 145);
		
		td = new Label(shell, SWT.NONE);
		td.setBounds(466, 255, 59, 14);
		
		td.setText(String.valueOf(fact.getTotaltotalConDesc()));
		
		label_2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setBounds(10, 302, 585, 2);
		
		btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.setBounds(278, 413, 106, 28);
		btnCancelar.setText("Cancelar");
		
		lblNumeroDeFactura = new Label(shell, SWT.NONE);
		lblNumeroDeFactura.setBounds(10, 2, 263, 14);
		lblNumeroDeFactura.setText("Numero de Factura:" +" " +  String.valueOf(fact.getPrimaryKey()));
		for(int i=0;i<fact.getArts().size();i++){
			TableItem t = new TableItem(table, SWT.NONE);
			
			t.setText(4, String.valueOf(fact.getPrimaryKey()));
			t.setText(0, fact.getArts().get(i).getArt().getName() );
			t.setText(1, String.valueOf(fact.getArts().get(i).getQuant()));
			t.setText(2, String.valueOf(fact.getArts().get(i).getUnitPrice()));
			t.setText(3, String.valueOf(fact.getArts().get(i).getTotalprice()));
		}
	}
}
